﻿#include "CPlcStandardClass.h"
using namespace plc;
//上升沿实现
CR_Trig::CR_Trig()
{
    M = false;
    Q = false;
}
CR_Trig::~CR_Trig()
{
}
void CR_Trig::execute(bool _IN)
{
    Q = (_IN & (!M));
    M = _IN;
}
bool CR_Trig::getQValue()
{
    return Q;
}

//下降沿实现
CF_Trig::CF_Trig()
{
    M = false;
    Q = false;
}
CF_Trig::~CF_Trig()
{
}
void CF_Trig::execute(bool _IN)
{
    Q = (!_IN) & M;
    M = _IN;
}
bool CF_Trig::getQValue()
{
    return Q;
}

//延时接通定时器实现
CTon::CTon()
{
    startTime = clock() * 1000 / CLOCKS_PER_SEC;
    ET        = 0;
    Q         = false;
    M         = false;
}
CTon::~CTon()
{
}
void CTon::execute(bool _IN, int32_t PT)
{
    if (!_IN)   //输入使能为false
    {
        startTime = clock() * 1000 / CLOCKS_PER_SEC;   //记录初始时间
        Q         = false;
        ET        = 0;
    }
    else
    {
        int32_t current_time = clock() * 1000 / CLOCKS_PER_SEC;
        ET                   = current_time - startTime;   //过去的时间
        if (ET >= PT)
        {
            ET = PT;
            Q  = true;
        }
        else
        {
            Q = false;
        }
    }
    M = _IN;
}
bool CTon::getQValue()
{
    return Q;
}
int32_t CTon::getETValue()
{
    return ET;
}

//延时断开定时器实现
CTof::CTof()
{
    startTime = clock() * 1000 / CLOCKS_PER_SEC;
    ET        = 0;
    Q         = false;
    M         = false;
}
CTof::~CTof()
{
}
void CTof::execute(bool _IN, int32_t PT)
{
    if (_IN)   //输入使能为false
    {
        startTime = clock() * 1000 / CLOCKS_PER_SEC;   //记录初始时间
        Q         = true;
        ET        = 0;
    }
    else if (Q)
    {
        int32_t current_time = clock() * 1000 / CLOCKS_PER_SEC;
        ET                   = current_time - startTime;   //过去的时间
        if (ET > PT)
        {
            ET = PT;
            Q  = false;
        }
    }
    else
    {
        startTime = 0;
        ET        = 0;
        Q         = false;
        M         = false;
    }
    M = _IN;
}
bool CTof::getQValue()
{
    return Q;
}
int32_t CTof::getETValue()
{
    return ET;
}

CTonr::CTonr()
{
    startTime = clock() * 1000 / CLOCKS_PER_SEC;
    ET        = 0;
    Q         = false;
    M         = false;
}

CTonr::~CTonr()
{
}

void CTonr::execute(bool _IN, int32_t PT)
{
    if (!_IN)   //输入使能为false
    {
        startTime = clock() * 1000 / CLOCKS_PER_SEC;   //记录初始时间
        M         = false;
        ET        = 0;
    }
    else
    {
        if (!M)
        {
            startTime = clock() * 1000 / CLOCKS_PER_SEC;   //记录初始时间
        }
        int32_t current_time = clock() * 1000 / CLOCKS_PER_SEC;
        ET += current_time - startTime;   //过去的时间
        startTime = current_time;
        M         = true;
        if (ET >= PT)
        {
            Q = true;
        }
        else
        {
            Q = false;
        }
    }
}

void CTonr::reset()
{
    ET = 0;
    M  = false;
    Q  = false;
}

bool CTonr::getQValue()
{
    return Q;
}

int32_t CTonr::getETValue()
{
    return ET;
}
